package com.fms.fmsapi.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fms.fmsapi.entity.*;
import com.fms.fmsapi.entity.param.NewOrder;
import com.fms.fmsapi.entity.result.OrderResult;
import com.fms.fmsapi.service.UserService;
import com.fms.fmsapi.service.impl.ContractServiceImpl;
import com.fms.fmsapi.service.impl.OrderServiceImpl;
import com.fms.fmsapi.service.impl.RoleServiceImpl;
import com.fms.fmsapi.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张力文
 * @since 2024-04-03
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderServiceImpl orderService;

    @Autowired
    ContractServiceImpl contractService;

    @Autowired
    UserService userService;

    @Autowired
    RoleServiceImpl roleService;

    @PutMapping("/queren")
    public Boolean queren(@RequestBody Order order){
        order.setStatus(8);
        boolean save = orderService.updateById(order);
        return save;
    }

    @PutMapping("/shoufei")
    public Boolean shoufei(@RequestBody Order order){
        order.setStatus(7);
        boolean save = orderService.updateById(order);
        return save;
    }

    @PutMapping("/anzhuang")
    public Boolean anzhuang(@RequestBody Order order){
        order.setStatus(6);
        boolean save = orderService.updateById(order);
        return save;
    }

    @PutMapping("/chuhuo")
    public Boolean chuhuo(@RequestBody Order order){
        order.setStatus(5);
        boolean save = orderService.updateById(order);
        return save;
    }

    @PutMapping("/dahui")
    public Boolean dahui(@RequestBody Order order){
        order.setStatus(4);
        boolean save = orderService.updateById(order);
        return save;
    }

    @PutMapping("/tongguo")
    public Boolean tongguo(@RequestBody Order order){
        order.setStatus(3);
        boolean save = orderService.updateById(order);
        return save;
    }

    @PutMapping("/wanshan")
    public Boolean wanshan(@RequestBody Order order){
        order.setStatus(2);
        boolean save = orderService.updateById(order);
        return save;
    }

    @GetMapping("/list")
    public R getList(@RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime startTime, @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime endTime, @RequestParam(required = false) Integer status, HttpServletRequest request){

        String token = JwtUtil.getToken(request);
        String userId = JwtUtil.getUserId(token);
        User user = userService.getById(userId);
        Role role = roleService.getById(user.getRoleId());
        String roleName = role!=null?role.getName():"暂无角色";
        LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<>();
        if(roleName.equals("暂无角色")||roleName.equals("销售人员")) {
            lqw.eq(Order::getSalemanId,user.getUuid());
        }
        if(status!=null){
            lqw.eq(Order::getStatus,status);
        }
        if(startTime!=null&&endTime!=null){
            lqw.between(Order::getCreateTime,startTime,endTime);
        }
        List<Order> list = orderService.list(lqw);
        List<OrderResult> results = new ArrayList<>();
        for(Order order : list){
            OrderResult result = new OrderResult();
            BeanUtils.copyProperties(order,result);
            User saleman = userService.getById(order.getSalemanId());
            result.setSalemanName(saleman.getNickName());
            results.add(result);
        }

        return R.success(results);
    }

    @PutMapping("/createOrder")
    @Transactional
    public R createOrder(@RequestBody NewOrder newOrder){
        Order order = new Order();
        Contract contract = new Contract();
        BeanUtils.copyProperties(newOrder,order);
        BeanUtils.copyProperties(newOrder,contract);
        UUID uuid = UUID.randomUUID();
        contract.setUuid(String.valueOf(uuid));
        boolean save = contractService.save(contract);
        boolean save1 = false;
        if(save){
            order.setContractId(String.valueOf(uuid));
            order.setStatus(1);
            order.setCreateTime(LocalDateTime.now());
            save1 = orderService.save(order);
        }
        if(save1){
            return R.fail(200,"创建订单成功");
        } else{
            return R.fail(300,"创建订单失败");
        }
    }


}

